package laba4;

import interfaces.task4.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author yakimovich
 */
public class CollectionUtilsImpl implements CollectionUtils {

    /**
     * Returns the union of two specified Collections
     *
     * @param clctn Collection for an union
     * @param clctn1 Collection for an union
     * @return the union of specified Collections
     */
    @Override
    public List<Integer> union(Collection<Integer> clctn, Collection<Integer> clctn1) {
        List<Integer> result = new ArrayList<>(clctn);
        for (Integer element : clctn1) {
            result.add(element);
        }
        return result;


//        List<Integer> result = new ArrayList<>(clctn);
//        Collection<Integer> temp = new ArrayList<>(clctn1);
//        for (Integer element : clctn) {
//            if (temp.contains(element)) {
//                temp.remove(element);
//            }
//        }
//        result.addAll(temp);
//        return result;
    }

    /**
     * Returns the intersection of two specified Collections
     *
     * @param clctn Collection for an intersection
     * @param clctn1 Collection for an intersection
     * @return the intersection of specified Collections
     */
    @Override
    public List<Integer> intersection(Collection<Integer> clctn, Collection<Integer> clctn1) {
        if (clctn == null || clctn1 == null) {
            throw new NullPointerException();
        }
        List<Integer> result = new ArrayList<>();
        for (Integer element : clctn) {
            if (clctn1.contains(element)) {
                result.add(element);
            }
        }
        for (Integer element : clctn1) {
            if (clctn.contains(element)) {
                result.add(element);
            }
        }
        return result;

//        List<Integer> result = new ArrayList<>();
//        Collection<Integer> temp = new ArrayList<>(clctn1);
//        for (Integer element : clctn) {
//            if (temp.contains(element)) {
//                temp.remove(element);
//                result.add(element);
//            }
//        }
//        return result;
    }

    /**
     * Returns the intersection without duplicate of two specified Collections
     *
     * @param clctn Collection for an intersection without duplicate
     * @param clctn1 Collection for an intersection without duplicate
     * @return the intersection without duplicate of specified Collections
     */
    @Override
    public Set<Integer> intersectionWithoutDuplicate(Collection<Integer> clctn, Collection<Integer> clctn1) {
        if (clctn == null || clctn1 == null) {
            throw new NullPointerException();
        }
        Set<Integer> result = new HashSet<>();
        for (Integer element : clctn) {
            if (clctn1.contains(element)) {
                result.add(element);
            }
        }
        return result;
    }

    /**
     * Returns the difference of two specified Collections
     *
     * @param clctn the first Collection
     * @param clctn1 the second Collection
     * @return the intersection of specified Collections
     */
    @Override
    public Collection<Integer> difference(Collection<Integer> clctn, Collection<Integer> clctn1) {
        Collection<Integer> result = new ArrayList<>(clctn);
        Collection<Integer> temp = new ArrayList<>(clctn1);
        for (Integer element : clctn) {
            if (temp.contains(element)) {
                temp.remove(element);
                result.remove(element);
            }
        }
        return result;
    }
}
